package com.shqcjd.sinopecdemomobile.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.shqcjd.sinopecdemomobile.R;
import com.shqcjd.sinopecdemomobile.model.pojo.KnowledgeBean;
import com.shqcjd.sinopecdemomobile.ui.activity.KnowledgeInfoActivity;
import com.shqcjd.sinopecdemomobile.util.Glideutil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class KnowledgeAdapter extends BaseQuickAdapter<KnowledgeBean.ListBean, KnowledgeAdapter.ViewHolder> {
    private Context context;

    public KnowledgeAdapter(@Nullable List<KnowledgeBean.ListBean> data, Context context) {
        super(R.layout.item_knowledge, data);
        this.context = context;
    }

    @Override
    protected void convert(KnowledgeAdapter.ViewHolder helper, KnowledgeBean.ListBean item) {
        RequestOptions options = new RequestOptions()
                .placeholder(R.mipmap.ic_default_small)    //加载成功之前占位图
                .error(R.mipmap.ic_default_small)    //加载错误之后的错误图
//                .override(109,68)	//指定图片的尺寸
                .centerCrop();//指定图片的缩放类型为centerCrop （等比例缩放图片，直到图片的宽高都

        if (item.videoUrl != null) {
            String suffix = item.videoUrl.substring(item.videoUrl.lastIndexOf(".")).toLowerCase();
            switch (suffix) {
                case ".doc":
                case ".docx":
                    Glide.with(mContext)
                            .load(R.mipmap.ic_doc)
                            .apply(options)
                            .into(helper.ivItemKnowledgePic);
                    break;
                case ".pdf":
                    Glide.with(mContext)
                            .load(R.mipmap.ic_pdf)
                            .apply(options)
                            .into(helper.ivItemKnowledgePic);
                    break;
                case ".jpg":
                case ".png":
                case ".jpeg":
                    Glide.with(mContext)
                            .load(item.videoUrl)
                            .apply(options)
                            .into(helper.ivItemKnowledgePic);
                    break;
                default:
                    Glideutil.loadImag(context, helper.ivItemKnowledgePic, item.imageUrl);
                    break;
            }
        }


        helper.tvItemKnowledgeTitle.setText(item.projectName);
        helper.tvItemKnowledgeName.setText(item.userName + "  " + item.timeLength);
        helper.tvItemKnowledgeTag.setText(item.tags);
        helper.tvItemKnowledgeTime.setText(item.uploadTime);
        helper.tvItemKnowledgeGrade.setText("满意度:" + item.satisfactionDegree);
        helper.btnItemKnowledge.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    Intent it = new Intent(context, KnowledgeInfoActivity.class);
                    it.putExtra("kId", item.knowledgeBaseId);
                    context.startActivity(it);
            }
        });

    }

    class ViewHolder extends BaseViewHolder {
        @BindView(R.id.btn_item_knowledge)
        Button btnItemKnowledge;
        @BindView(R.id.tv_item_knowledge_title)
        TextView tvItemKnowledgeTitle;
        @BindView(R.id.tv_item_knowledge_name)
        TextView tvItemKnowledgeName;
        @BindView(R.id.tv_item_knowledge_tag)
        TextView tvItemKnowledgeTag;
        @BindView(R.id.tv_item_knowledge_time)
        TextView tvItemKnowledgeTime;
        @BindView(R.id.tv_item_knowledge_grade)
        TextView tvItemKnowledgeGrade;
        @BindView(R.id.iv_item_knowledge_pic)
        ImageView ivItemKnowledgePic;
        @BindView(R.id.tv_item_knowledge_pos)
        TextView tvItemKnowledgePos;


        public ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

}
